package BandB.Tool.GPhoneManager;

import BandB.Tool.GPhoneManager.Widget.GPhoneWidget;
import BandB.Tool.GPhoneManager.Widget.AndroidBrightnessControl.AndroidBrightnessControl;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioManager;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Toggles extends Activity {

	public static final String KEY_AIRPLANE_MODE_STATE = "airplane_mode_state";

	public static final String ACTION_AIRPLANE_MODE_STATE = "com.mgeek.android.airplane.state.action";
	public static final String KEY_PROCEING_STATE = "processing_state";
	public static final int PROCESSING_STATE_TURNING_ON = 200;
	public static final int PROCESSING_STATE_TURNING_OFF = 220;

	protected static final String TAG = "Toggles";

	private Button airplaneon;
	private Button airplaneoff;
	private Button wifion;
	private Button wifioff;
	private WifiManager wifiManager;

	private Button brightness;

	private Button silentmodelon;
	private Button silentmodeloff;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Implement this method.
		super.onCreate(savedInstanceState);
		setContentView(R.layout.toggles);
		wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
		initview();

		Log.e("water", "enter togglers");
	}

	private void initview() {
		// TODO Implement this method.
		int isEnabled = Settings.System.getInt(getContentResolver(),
				Settings.System.AIRPLANE_MODE_ON, 0);
		airplaneon = (Button) findViewById(R.id.airplanemodeon);
		airplaneoff = (Button) findViewById(R.id.airplanemodeoff);
		if (isEnabled == 1) {
			/*
			 * 这里是当飞行模式打开的时候
			 */
			initairplaneon();

		} else {
			initairplaneoff();

		}

		wifion = (Button) findViewById(R.id.wifion);
		wifioff = (Button) findViewById(R.id.wifioff);
		if (wifiManager.isWifiEnabled()) {

			initwifion();

		} else {
			initwifioff();

		}

		brightness = (Button) findViewById(R.id.brightness);
		brightness.setOnClickListener(onClickListener);

		AudioManager audioManager = (AudioManager) this
				.getSystemService(Context.AUDIO_SERVICE);
		int mode = audioManager.getRingerMode();

		silentmodelon = (Button) findViewById(R.id.silentmodelon);
		silentmodeloff = (Button) findViewById(R.id.silentmodeloff);

		if (mode == AudioManager.RINGER_MODE_SILENT) {
			initsilentoff();
			
			
			
		} else {
			initsilenton();
			
		}

	}

	private void initsilenton() {
		// TODO Implement this method.
		silentmodelon.setBackgroundResource(R.drawable.yes);
		silentmodeloff.setBackgroundResource(R.drawable.no);
		silentmodelon.setClickable(false);
		silentmodeloff.setClickable(true);
		silentmodeloff.setOnClickListener(onClickListener);
	}

	private void initsilentoff() {
		// TODO Implement this method.
		silentmodelon.setBackgroundResource(R.drawable.no);
		silentmodeloff.setBackgroundResource(R.drawable.yes);
		silentmodelon.setClickable(true);
		silentmodeloff.setClickable(false);
		silentmodelon.setOnClickListener(onClickListener);
	}

	private void initwifioff() {
		// TODO Implement this method.
		wifion.setBackgroundResource(R.drawable.no);
		wifioff.setBackgroundResource(R.drawable.yes);
		wifion.setClickable(true);
		wifioff.setClickable(false);
		wifion.setOnClickListener(onClickListener);
	}

	private void initwifion() {
		// TODO Implement this method.
		wifion.setBackgroundResource(R.drawable.yes);
		wifioff.setBackgroundResource(R.drawable.no);
		wifion.setClickable(false);
		wifioff.setClickable(true);
		wifioff.setOnClickListener(onClickListener);
	}

	private void initairplaneoff() {
		// TODO Implement this method.
		airplaneon.setBackgroundResource(R.drawable.no);
		airplaneoff.setBackgroundResource(R.drawable.yes);
		airplaneon.setClickable(true);
		airplaneoff.setClickable(false);
		airplaneon.setOnClickListener(onClickListener);
	}

	private void initairplaneon() {
		// TODO Implement this method.
		airplaneon.setBackgroundResource(R.drawable.yes);
		airplaneoff.setBackgroundResource(R.drawable.no);
		airplaneon.setClickable(false);
		airplaneoff.setClickable(true);
		airplaneoff.setOnClickListener(onClickListener);
	}

	private final OnClickListener onClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Implement this method.
			switch (v.getId()) {
			case R.id.airplanemodeon:
				initairplaneon();
				Intent turningOnIntent = new Intent();
				turningOnIntent.setAction(ACTION_AIRPLANE_MODE_STATE);
				turningOnIntent.putExtra(KEY_PROCEING_STATE,
						PROCESSING_STATE_TURNING_ON);
				sendBroadcast(turningOnIntent);

				Settings.System.putInt(getContentResolver(),
						Settings.System.AIRPLANE_MODE_ON, 1);

				setservice();
				break;
			case R.id.airplanemodeoff:
				initairplaneoff();
				Intent turningoffIntent = new Intent();
				turningoffIntent.setAction(ACTION_AIRPLANE_MODE_STATE);
				turningoffIntent.putExtra(KEY_PROCEING_STATE,
						PROCESSING_STATE_TURNING_OFF);
				sendBroadcast(turningoffIntent);
				Settings.System.putInt(getContentResolver(),
						Settings.System.AIRPLANE_MODE_ON, 0);
				setservice();
				break;
			case R.id.wifion:
				initwifion();

				wifiManager.setWifiEnabled(true);
				Log.w(TAG, "Has turned on wifi");
				wifiManager.reconnect();
				wifiManager.reassociate();
				break;
			case R.id.wifioff:
				initwifioff();
				wifiManager.setWifiEnabled(false);
				Log.w(TAG, "Has shut down wifi");
				break;
			case R.id.brightness:
				
				Intent intent = new Intent();
				intent.setClass(Toggles.this, AndroidBrightnessControl.class);
				startActivity(intent);
				break;

			case R.id.silentmodelon:
				initsilenton();
				Intent intent2 = new Intent();
				intent2.setAction(GPhoneWidget.ACTION_SILENT_CHANGED);
				intent2.putExtra("sender", "toggles");
				sendBroadcast(intent2);
				break;
			case R.id.silentmodeloff:
				initsilentoff();
				Intent intent3 = new Intent();
				intent3.setAction(GPhoneWidget.ACTION_SILENT_CHANGED);
				intent3.putExtra("sender", "toggles");
				sendBroadcast(intent3);

				break;

			default:
				break;
			}
		}

		private void setservice() {
			// TODO Implement this method.
			Boolean isEnabled = Settings.System.getInt(getContentResolver(),
					Settings.System.AIRPLANE_MODE_ON, 0) == 1;
			IntentFilter intentFilter = new IntentFilter();
			intentFilter.addAction("android.intent.action.SERVICE_STATE");
			intentFilter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);

			// Post an intent to reload
			Intent airplaneIntent = new Intent();
			airplaneIntent.putExtra(KEY_AIRPLANE_MODE_STATE, isEnabled);
			sendBroadcast(airplaneIntent);

			Intent airplaneIntent2 = new Intent();
			airplaneIntent2.setAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
			sendBroadcast(airplaneIntent2);
		}

	};

}
